﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
namespace CopyPathFile
{
    class Configuration
    {
        public Configuration()
        {
            try
            {
                string[] subkeyNames;
                bool _exit = false;
                RegistryKey hkml = Registry.LocalMachine;
                RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
                subkeyNames = software.GetSubKeyNames();
                foreach (string keyName in subkeyNames)
                {
                    if (keyName == "www.webasks.com")
                    {
                        _exit = true;
                        break;
                    }
                }
                if (!_exit)
                {
                    RegistryKey webasks = software.CreateSubKey("www.webasks.com");
                    webasks.SetValue("Source","");
                    webasks.SetValue("Target", "");
                    webasks.SetValue("Filter", "");
                    webasks.SetValue("LatestTime", "");
                }
            }
            catch
            { }
        }
        public string Source
        {
            get
            {
                return GetRegistData("Source");
            }
            set { EditRegedit("Source",value); }
        }
        public string Target
        {
            get
            {
                return GetRegistData("Target");
            }
            set { EditRegedit("Target", value); }
        }
        public string Filter
        {
            get
            {
                return GetRegistData("Filter");
            }
            set { EditRegedit("Filter", value); }
        }
        public string LatestTime
        {
            get
            {
                return GetRegistData("LatestTime");
            }
            set { EditRegedit("LatestTime", value); }
        }

        /// <summary>
        /// 读取的注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下的XXX目录中名称为name的注册表值；
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private string GetRegistData(string name)
        {
            string registData;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.OpenSubKey("www.webasks.com", false);
            registData = aimdir.GetValue(name).ToString();
            return registData;
        }

        /// <summary>
        /// 在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下新建XXX目录并在此目录下创建名称为name值为tovalue的注册表项；
        /// </summary>
        /// <param name="name"></param>
        /// <param name="tovalue"></param>
        private void EditRegedit(string name, string value)
        {
            RegistryKey hklm = Registry.LocalMachine;
            RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.OpenSubKey("www.webasks.com",true);
            aimdir.SetValue(name, value);
        }

        /// <summary>
        /// 在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下XXX目录中删除名称为name注册表项；
        /// </summary>
        /// <param name="name"></param>
        private void DeleteRegist(string name)
        {
            string[] aimnames;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.OpenSubKey("www.webasks.com", true);
            aimnames = aimdir.GetSubKeyNames();
            foreach (string aimKey in aimnames)
            {
                if (aimKey == name)
                    aimdir.DeleteSubKeyTree(name);
            }
        }

        /// <summary>
        /// 在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下XXX目录中判断名称为name注册表项是否存在，这一方法在删除注册表时已经存在，在新建一注册表项时也应有相应判断；  
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private bool IsRegeditExit(string name)
        {
            bool _exit = false;
            try
            {
                string[] subkeyNames;
                RegistryKey hkml = Registry.LocalMachine;
                RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
                RegistryKey aimdir = software.OpenSubKey("www.webasks.com", true);
                subkeyNames = aimdir.GetValueNames();
                foreach (string keyName in subkeyNames)
                {
                    if (keyName == name)
                    {
                        _exit = true;
                        return _exit;
                    }
                }
            }
            catch
            { }
            return _exit;
        }

    }
}
